<?php
    class PayRes extends APIBase {
		
		public $tag = "PayRes";
		public $isLogin = true;
		public $goodsconf = array(
				6=>'end_time1',
				7=>'end_time2',
				8=>'end_time3',
				10=>'end_time4'
		);
		public $oldconf = array(
				5=>'end_time1',
				10=>'end_time2',
				15=>'end_time2',
				20=>'end_time2',
				30=>'end_time3'
		);
		
		public function before() {
			$this->initMysql();
			return true;
		}
		
		
		public function logic() {
			define('TIME_STAMP' , (int)time());
			if (!isset($this->param['orderid'])) {
				$this->returnError(310, "orderid must be needed");
			}
			
			$orderid = $this->param['orderid'];
			$where['orderid'] = $orderid;
			$sign  = $this->mysql->select('applypay', '*' , $where);
			$price = (int)($sign[0]['price']);
			//取得道具id
			$data = array();
			$data['message'] = $sign[0]['type'];
			if ($data['message'] == 1){
				if ((int)$sign[0]['version'] >= 11){
					//新版本
					$gid = (int)($sign[0]['goodsid']);
					
					if ( ($gid == 6) || ($gid == 7)  || ($gid == 8)){
						$congid = $this->goodsconf[$gid];
						$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
						$gid == 8  ?  define('LIFE_TIME', 86400*7)  :  define('LIFE_TIME', 86400);
						$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
						$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
					}elseif ($gid == 10){
						$congid = $this->goodsconf[$gid];
						$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
						define('LIFE_TIME', 86400*31);
						$cur_vsign = $this->data_redis->hincrBY("hu:{$this->uid}", 'vsign', 1);
						//两个30生效
						if($cur_vsign % 2 == 0){
							$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
							$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
						}
					}
				}else{
					//老版本
					if ($price >= 5){
						$congid = $this->oldconf[$price];
						$cur_data =  $this->data_redis->hMGet("hu:{$this->uid}", array($congid));
						
						if ( $price == 30){
							//30元处理
							if ($cur_data[$congid] > TIME_STAMP){
								//end_time3有效期内
								define('LIFE_TIME', 86400*31);
								$next_data = $this->data_redis->hMGet("hu:{$this->uid}", array('end_time4'));
								$next_data['end_time4'] > TIME_STAMP  ?   $next_data['end_time4'] += LIFE_TIME  :  $next_data['end_time4'] = TIME_STAMP + LIFE_TIME;
								$this->data_redis->hset("hu:{$this->uid}", $congid, 0);
								$this->data_redis->hset("hu:{$this->uid}", 'end_time4', $next_data['end_time4']);
// 								echo $cur_data['end_time4']."---".$congid;
							}else{
								define('LIFE_TIME', 86400*7);
								$cur_data[$congid] = TIME_STAMP + LIFE_TIME;
								$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
							}
							
						} else {
							//低于30元
							define('LIFE_TIME', 86400);
							$cur_data[$congid] > TIME_STAMP  ?   $cur_data[$congid] += LIFE_TIME  :  $cur_data[$congid] = TIME_STAMP + LIFE_TIME;
							$this->data_redis->hset("hu:{$this->uid}", $congid, $cur_data[$congid]);
						}
						
					}
				}
				$cur_nums = $this->data_redis->hMGet("hu:{$this->uid}", array("money", "rmb", "coin"));
				$data['money'] = (int)$cur_nums['money'];
				$data['rmb'] = (int)$cur_nums['rmb'];
				$data['coin'] = (int)$cur_nums['coin'];
			}
			$this->returnData($data);
		}
		
		public function after() {
			$this->deinitMysql();
			$this->deinitDataRedis();
			
		}
    }
?>